<template>
    <div class="count-down" v-html="countDown(endDate)">
    </div>
</template>

<script lang="ts">
import { Component, Prop, Vue, Emit } from "vue-property-decorator"
import moment from 'moment'

@Component
export default class CountDown extends Vue {
    @Prop() endDate!: string
    // 变量后加!是非空断言

    now: any = moment()
    mounted() {
        setInterval((): void =>{
            this.now = moment()
        },1000)
    }
    destroyed() {
        
    }
    get countDown(): object{
        return function(endDate: any): string {
            let m1: any = this.now
            let m2: any = moment(endDate)
            let du: any = moment.duration(m2 - m1, 'ms')
            let hours: number = du.get('hours')
            let mins: number = du.get('minutes')
            let ss: number = du.get('seconds')
            if(hours <= 0 && mins <= 0 && ss <= 0) {
                // this.$emit('timeout')
                this.timeout()
                return "今日已结束"
            }else {
                return `${this.PrefixInteger(hours,2)} <span style="font-size: 16px;">小时</span> ${this.PrefixInteger(mins,2)} <span style="font-size: 16px;">分钟</span><span style="color: #F56C6C;"> ${this.PrefixInteger(ss,2)} </span><span style="font-size: 16px;">秒</span>`
            }
        }
    }

    @Emit()
    timeout(){}

    
    //数字前补 0 
    // num传入的数字，n需要的字符长度
    PrefixInteger(num: number, n: number): string {
        return (Array(n).join('0') + num).slice(-n)
    }
}
</script>

<style lang="less" scoped>
.count-down{
    font-family: "Helvetica Neue";
    text-align: center;
    font-size: 26px;
}
</style>